pacman:: p_load(sf, spNetwork, tmap, tidyverse)Hands-on Exercise 3
This webpage documents Hands-on Exercise 03.
3.1 Installation and Loading of Packages
3.2 Importation and preparation of the data
network<- st_read(dsn = "data/geospatial", layer ="Punggol_St") %>%
st_transform( crs = 3414)Reading layer `Punggol_St' from data source
`C:\dewschan\ISSS622-GAA\Hands-on_Ex\Hands-on_Ex03\data\geospatial'
using driver `ESRI Shapefile'
Simple feature collection with 2642 features and 2 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: 34038.56 ymin: 40941.11 xmax: 38882.85 ymax: 44801.27
Projected CRS: SVY21 / Singapore TM
childcare <- st_read (dsn= "data/geospatial", layer="Punggol_CC") %>%
st_transform( crs = 3414) %>% st_zm()Reading layer `Punggol_CC' from data source
`C:\dewschan\ISSS622-GAA\Hands-on_Ex\Hands-on_Ex03\data\geospatial'
using driver `ESRI Shapefile'
Simple feature collection with 61 features and 1 field
Geometry type: POINT
Dimension: XYZ
Bounding box: xmin: 34423.98 ymin: 41503.6 xmax: 37619.47 ymax: 44685.77
z_range: zmin: 0 zmax: 0
Projected CRS: SVY21 / Singapore TM
3.3 Visualising the Geospatial Data
plot(st_geometry(network))
plot(childcare, add=T, col ='red', pch = 19)
visualization using the Tmap package.
tmap_mode('view')tmap mode set to interactive viewing
tm_shape(childcare) +
tm_dots() +
tm_shape(network) +
tm_lines()changing to tmap plot
tmap_mode('plot')tmap mode set to plotting
3.4 Network KDE (NKDE) Analysis
3.4.1 Preparing the lixels Objects
Before computing NKDE, the SpatialLines object need to be cut into lixels with a specified minimal distance. This task can be performed by using with lixelize_lines() of spNetwork as shown in the code chunk below.
lixels <- lixelize_lines(network,
700,
mindist = 375)The length of a lixel, lx_length is set to 700m, and The minimum length of a lixel, mindist is set to 350m. After cut, if the length of the final lixel is shorter than the minimum distance, then it is added to the previous lixel. If NULL, then mindist = maxdist/10. Also note that the segments that are already shorter than the minimum distance are not modified
Note: There is another function called lixelize_lines.mc() which provide multicore support.
3.4.2 Generating line centre points
Next, lines_center() of spNetwork will be used to generate a SpatialPointsDataFrame (i.e. samples) with line centre points as shown in the code chunk below.
samples <- lines_center(lixels)3.4.3 Performing NKDE
The following code chunk is used to perform NKDE.
kernel_name argument indicates that quartic kernel is used. Are possible kernel methods supported by spNetwork are: triangle, gaussian, scaled gaussian, tricube, cosine ,triweight, epanechnikov or uniform.
method argument indicates that simple method is used to calculate the NKDE. Currently, spNetwork support three popular methods, they are: method=“simple”. This first method was presented by Xie et al. (2008) and proposes an intuitive solution. The distances between events and sampling points are replaced by network distances, and the formula of the kernel is adapted to calculate the density over a linear unit instead of an areal unit.
method=“discontinuous”. The method is proposed by Okabe et al (2008), which equally “divides” the mass density of an event at intersections of lixels.
method=“continuous”. If the discontinuous method is unbiased, it leads to a discontinuous kernel function which is a bit counter-intuitive. Okabe et al (2008) proposed another version of the kernel, that divide the mass of the density at intersection but adjusts the density before the intersection to make the function continuous.
As the childcare is in 3D different geometry, it is converted to 2D using the following code.
# Convert geometry to 2D by dropping Z and M dimensions
childcare_2<- st_zm(childcare, drop = TRUE, what = "ZM")#performing NkDE using the code
densities <- nkde(network,
events = childcare_2,
w = rep(1, nrow(childcare_2)),
samples = samples,
kernel_name = "quartic",
bw = 300,
div= "bw",
method = "simple",
digits = 1,
tol = 1,
grid_shape = c(1,1),
max_depth = 8,
agg = 5,
sparse = TRUE,
verbose = FALSE)#
densities_dis <- nkde(network,
events = childcare_2,
w = rep(1, nrow(childcare_2)),
samples = samples,
kernel_name = "quartic",
bw = 300,
div= "bw",
method = "discontinuous",
digits = 1,
tol = 1,
grid_shape = c(1,1),
max_depth = 8,
agg = 5,
sparse = TRUE,
verbose = FALSE)#
densities_con <- nkde(network,
events = childcare_2,
w = rep(1, nrow(childcare_2)),
samples = samples,
kernel_name = "quartic",
bw = 300,
div= "bw",
method = "continuous",
digits = 1,
tol = 1,
grid_shape = c(1,1),
max_depth = 8,
agg = 5,
sparse = TRUE,
verbose = FALSE)3.4.4 Visualisaing NKDE
samples$density <- densities
lixels$density <- densities# re-scaling to help the mapping
samples$density <- samples$density*10000
lixels$density <- lixels$density*10000# To prepare interactive and high cartographic quality map visualization
tmap_mode('view')tmap mode set to interactive viewing
tm_shape(lixels) +
tm_lines(col = "density") +
tm_shape(childcare)+
tm_dots()tmap_mode('plot')tmap mode set to plotting
#adding density derived from discontinuous and continuous method to the samples and lixels
samples$density_dis <- densities_dis
lixels$density_dis <- densities_dis
samples$density_con <- densities_con
lixels$density_con <- densities_con
# re-scaling to help the mapping
samples$density_dis <- samples$density_dis*10000
lixels$density_dis <- lixels$density_dis*10000
samples$density_con <- samples$density_con*10000
lixels$density_con <- lixels$density_con*10000# To prepare interactive and high cartographic quality map visualization for discontinuous method
tmap_mode('view')tmap mode set to interactive viewing
tm_shape(lixels) +
tm_lines(col = "density_dis") +
tm_shape(childcare)+
tm_dots()tmap_mode('plot')tmap mode set to plotting
# To prepare interactive and high cartographic quality map visualization for Continuous method
tmap_mode('view')tmap mode set to interactive viewing
tm_shape(lixels) +
tm_lines(col = "density_con") +
tm_shape(childcare)+
tm_dots()tmap_mode('plot')tmap mode set to plotting
3.5 Network Constrained G- and K-Function Analysis
The complete spatial randomness (CSR) test by using kfunctions() of spNetwork package. The null hypothesis is defined as:
Ho: The observed spatial point events (i.e distribution of childcare centres) are uniformly distributed over a street network in Punggol Planning Area.
The CSR test is based on the assumption of the binomial point process which implies the hypothesis that the childcare centres are randomly and independently distributed over the street network.
If this hypothesis is rejected, we may infer that the distribution of childcare centres are spatially interacting and dependent on each other; as a result, they may form nonrandom patterns.
There are ten arguments used in the code chunk above they are:
lines: A SpatialLinesDataFrame with the sampling points. The geometries must be a SpatialLinesDataFrame (may crash if some geometries are invalid). points: A SpatialPointsDataFrame representing the points on the network. These points will be snapped on the network. start: A double, the start value for evaluating the k and g functions. end: A double, the last value for evaluating the k and g functions. step: A double, the jump between two evaluations of the k and g function. width: The width of each donut for the g-function. nsim: An integer indicating the number of Monte Carlo simulations required. In the above example, 50 simulation was performed. Note: most of the time, more simulations are required for inference resolution: When simulating random points on the network, selecting a resolution will reduce greatly the calculation time. When resolution is null the random points can occur everywhere on the graph. If a value is specified, the edges are split according to this value and the random points are selected vertices on the new network. conf_int: A double indicating the width confidence interval (default = 0.05).
#k function
kfun_childcare <- kfunctions(network,
childcare_2,
start = 0,
end = 1000,
step = 50,
width = 50,
nsim = 50,
resolution = 50,
verbose = FALSE,
conf_int = 0.05)
kfun_childcare$plotk